/* GUI CONVERTED from rcshwbe.p (converter v1.76) Tue Jul 16 00:36:03 2002 */
/* rcshwbe.p - Shipper Workbench Sub Routine                             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                   */
/* All rights reserved worldwide.  This is an unpublished work.          */
/* $Revision: 1.9.1.6 $                                                         */
/*V8:ConvertMode=Maintenance                                             */
/* REVISION: 7.5    LAST MODIFIED: 06/18/95           BY: GWM  *J049*    */
/* REVISION: 8.5    LAST MODIFIED: 04/24/96           BY: GWM  *J0K9*    */
/* REVISION: 8.6    LAST MODIFIED: 07/29/96   BY: *K003* Vinay Nayak-Sujir */
/* REVISION: 8.6    LAST MODIFIED: 12/24/96   BY: *K03K* Vinay Nayak-Sujir */
/* REVISION: 8.6    LAST MODIFIED: 03/19/97   BY: *J1LH* Jim Williams      */
/* REVISION: 8.6    LAST MODIFIED: 04/21/97   BY: *K0C2* Verghese Kurien   */
/* REVISION: 8.6    LAST MODIFIED: 04/25/97   BY: *K0C9* Taek-Soo Chang    */
/* REVISION: 8.5    LAST MODIFIED: 09/30/97   BY: *J221* John Worden       */

/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan      */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan      */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane*/
/* REVISION: 9.1      LAST MODIFIED: 04/13/00   BY: *K250* Surekha Joshi   */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* myb             */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Old ECO marker removed, but no ECO header exists *N1P0*                    */
/* $Revision: 1.9.1.6 $    BY: Seema Tyagi           DATE: 07/15/02  ECO: *N1P0*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE rcshwbe_p_1 "Merge Pre-Shipper"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

{rcinvtbl.i new}

/* INPUT PARAMETERS */
define input parameter abs_recid    as   recid.
define input parameter absfmt       like abs_format no-undo.

define variable cons_ok      as   logical   no-undo.
define variable other_so     like so_nbr    no-undo.
define variable carr_cdl1    as   character no-undo.
define variable carr_cdl2    as   character no-undo.
define variable ctr          as   integer   no-undo.
define variable can_discard  as   logical   no-undo.
define variable errorst      as   logical   no-undo.
define variable errornum     as   integer   no-undo.

/* LOCAL VARIABLES */
define variable ship_id        like abs_id       no-undo.
define variable form_code      like df_form_code no-undo.
define variable old_form_code  like df_form_code no-undo.
define variable conv_rate      as   decimal      no-undo.
define variable hdr_ok         like mfc_logical  no-undo.
define variable l_abs_tare_wt  like abs_nwt      no-undo.
define variable l_abs_tare_wt1 like abs_nwt      no-undo.

/* BUFFERS */
define buffer abs_temp  for abs_mstr.
define buffer abs_temp2 for abs_mstr.


/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
ship_id label {&rcshwbe_p_1} colon 19
   space(4)
 SKIP(.4)  /*GUI*/
with frame a side-labels attr-space centered overlay row 12 NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).


MAIN:
do transaction on endkey undo MAIN, leave MAIN:
/*GUI*/ if global-beam-me-up then undo, leave.


   find
      abs_mstr
      where recid(abs_mstr) = abs_recid exclusive-lock.

   set
      ship_id
   with frame a.

   find abs_temp
      where abs_temp.abs_shipfrom = abs_mstr.abs_shipfrom
        and abs_temp.abs_id       = "p" + ship_id
   exclusive-lock no-error.

   if locked(abs_temp)
   then do:
      /* RECORD LOCKED - TRY AGAIN LATER */
      {pxmsg.i &MSGNUM=7422 &ERRORLEVEL=3}
      undo MAIN, retry MAIN.
   end. /* IF locked(abs_temp) */

   if not available abs_temp
   then do:
      /* PICKLIST DOES NOT EXIST */
      {pxmsg.i &MSGNUM=772 &ERRORLEVEL=3}
      undo MAIN, retry MAIN.
   end. /* IF NOT AVAILABLE abs_temp */

   /* TRAP MERGE SELF ERROR */
   if recid(abs_mstr) = recid(abs_temp)
   then do:
      /* YOU CANNOT MERGE THIS PICKLIST */
      {pxmsg.i &MSGNUM=774 &ERRORLEVEL=3}
      undo MAIN, retry MAIN.
   end. /* IF recid(abs_mstr) = recid(abs_temp) */

   if abs_temp.abs_canceled = yes
   then do:
      /* SHIPPER CANCELED */
      {pxmsg.i &MSGNUM=5885 &ERRORLEVEL=3}
      undo MAIN, retry MAIN.
   end. /* IF abs_temp.abs_canceled = yes */

   if abs_mstr.abs_shipto <> abs_temp.abs_shipto
   then do:
      /* MERGED PICKLIST SHIPTO MUST BE EQUAL */
      {pxmsg.i &MSGNUM=767 &ERRORLEVEL=3}
      undo MAIN, retry MAIN.
   end. /* IF abs_mstr.abs_shipto <> abs_temp.abs_shipto */

   /* IF THE NR SEQUENCE DOES NOT PERMIT GAPS, THEN MERGE IS NOT PERMITTED */
   if abs_temp.abs_nr_id <> ""
   then do:
      run chk_delnbr
         (input  abs_temp.abs_nr_id,
          output can_discard,
          output errorst,
          output errornum).

      if errorst
      then do:
         {pxmsg.i &MSGNUM=errornum &ERRORLEVEL=3}
         undo MAIN, retry MAIN.
      end. /* IF errorst */

      if not can_discard
      then do:
         /* MERGE PROHIBITED, GAPS IN SEQUENCE NOT PERMITTED */
         {pxmsg.i &MSGNUM=5942 &ERRORLEVEL=3}
         undo MAIN, retry MAIN.
      end. /* IF NOT can_discard */

   end. /* IF abs_temp.abs_nr_id <> "" */

   /* IF INVOICE CONSOLIDATION IS TO HAPPEN, THEN CHECK FOR */
   /* COMPATIBILITY OF SOs ON THE SHIPPER                   */
   if can-find (first df_mstr
                   where df_format = absfmt
                     and df_type   = "1"
                     and df_inv    = true)
   then do:
      run chk_abs_inv_cons
         (input abs_mstr.abs_id,
          input abs_mstr.abs_shipfrom,
          input abs_temp.abs_id,
          input abs_temp.abs_shipfrom,
          "",
          output cons_ok).

      if cons_ok = false
      then do:
         /*INVALID SHIPPER. SALES ORDERS PROHIBIT INVOICE CONSOLIDATION*/
         {pxmsg.i &MSGNUM=5999 &ERRORLEVEL=3}
         undo MAIN, retry MAIN.
      end. /* IF cons_ok = false */

   end. /* IF CAN-FIND FIRST df_mstr  */


   /* IF SHIPPER CONSOLIDATION IS PROHIBITED THEN CHECK WHETHER */
   /* WE ARE VIOLATING THIS BY ADDING MORE THAN ONE SO TO THE   */
   /* BIGGER SHIPPER STRUCTURE                                  */
   if abs_mstr.abs_cons_ship = "0"
   then do:
      run chk_abs_shp_cons
         (input abs_mstr.abs_id,
          input abs_mstr.abs_shipfrom,
          input abs_temp.abs_id,
          input abs_temp.abs_shipfrom,
      input "",
          output cons_ok,
          output other_so).

      if not cons_ok
      then do:
         /*MULTIPLE SALES ORDERS ON SHIPPER. CONSOLIDATION NOT PERMITTED*/
         {pxmsg.i &MSGNUM=5827 &ERRORLEVEL=3}
         undo MAIN, retry MAIN.
      end. /* IF NOT cons_ok */

   end. /* IF abs_mstr.abs_cons_ship = "0" */

   /* COMPARE ALL HEADER ATTIRBUTES OF THE TWO SHIPPERS AND WARN IF DIFFERENT */
   if substring(abs_mstr.abs__qad01,1,20)    <>
      /* Ship Via */
      substring(abs_temp.abs__qad01,1,20)
   or substring(abs_mstr.abs__qad01,21,20)   <>
      /* FOB */
      substring(abs_temp.abs__qad01,21,20)
   or substring(abs_mstr.abs__qad01,41,20)   <>
      /* Trans Mode */
      substring(abs_temp.abs__qad01,41,20)
   or substring(abs_mstr.abs__qad01,61,20)   <>
      /* Carr Ref */
      substring(abs_temp.abs__qad01,61,20)
   or substring(abs_mstr.abs__qad01,81,20)   <>
      /* Veh Ref */
      substring(abs_temp.abs__qad01,81,20)
   or abs_mstr.abs_format     <> abs_temp.abs_format
   or abs_mstr.abs_cons_ship  <> abs_temp.abs_cons_ship
   or abs_mstr.abs_lang       <> abs_temp.abs_lang
   then do:
      /* HEADER FIELD(S) OF SHIPPER HAVE DIFFERENT VALUES. CONTINUE? */
      {pxmsg.i &MSGNUM=1775 &ERRORLEVEL=2 &CONFIRM=hdr_ok
               &CONFIRM-TYPE='LOGICAL'}

      if not hdr_ok
      then
         undo MAIN, retry MAIN.
   end. /* IF substring(abs_temp.abs_qad01 ... */

   /* IF MOST ATTRIBS MATCH */
   else do:
      /* BUILD COMMA-DELIMITED LIST OF CARRIERS AND COMPARE */
      for each absc_det
         fields(absc_abs_id absc_carrier)
         where absc_det.absc_abs_id = abs_mstr.abs_id
         no-lock:
         carr_cdl1 = "," + absc_carrier.
      end. /* FOR EACH absc_det */

      for each absc_det
         fields(absc_abs_id absc_carrier)
         where absc_det.absc_abs_id = abs_temp.abs_id
         no-lock:
         carr_cdl2 = "," + absc_carrier.
      end. /* FOR EACH absc_det */

      if num-entries(carr_cdl1) <> num-entries(carr_cdl2)
      then do:
         /* HEADER FIELD(S) OF SHIPPER HAVE DIFFERENT VALUES. CONTINUE? */
         {pxmsg.i &MSGNUM=1775 &ERRORLEVEL=2 &CONFIRM=hdr_ok
                  &CONFIRM-TYPE='LOGICAL'}
         if not hdr_ok
         then
            undo MAIN, retry MAIN.
      end. /* IF num-entries(carr_cdl1) ... */
      else
         do ctr = 1 to num-entries(carr_cdl1):
            if lookup (entry(ctr,carr_cdl1), carr_cdl2) = 0
            then do:
               /* HEADER FIELD(S) OF SHIPPER HAVE DIFFERENT VALUES. CONTINUE? */
               {pxmsg.i &MSGNUM=1775 &ERRORLEVEL=2 &CONFIRM=hdr_ok
                        &CONFIRM-TYPE='LOGICAL'}
               if not hdr_ok
               then
                  undo MAIN, retry MAIN.
               leave.
            end. /* IF LOOKUP(ENTRY(ctr,carr_cdl1), carr_cdl2) = 0 */
         end. /* DO ctr = 1 ...*/

   end. /* ELSE DO IF MOST ATTRIBS MATCH */

   for first df_mstr
      fields(df_format df_form_code df_inv df_type)
      where df_format = abs_mstr.abs_format
        and df_type   = "1"
      no-lock:
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* FOR FIRST df_mstr */

   old_form_code = if available df_mstr
                   then
                      df_form_code
                   else
                      "".

   for first df_mstr
      fields(df_format df_form_code df_inv df_type)
      where df_format = abs_temp.abs_format
        and df_type   = "1"
      no-lock:
   end. /* FOR FIRST df_mstr */

   form_code = if available df_mstr
               then
                  df_form_code
               else
                  "".

   if old_form_code <> form_code
   then do:
      {gprun.i ""sofsde.p"" "(input recid(abs_temp))"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end. /* IF old_form_code <> form_code */

   for each abs_temp2
      where abs_temp2.abs_shipfrom = abs_temp.abs_shipfrom
        and abs_temp2.abs_par_id   = abs_temp.abs_id
        exclusive-lock:
      abs_temp2.abs_par_id = abs_mstr.abs_id.
   end. /* FOR EACH abs_temp2 */

   conv_rate = 1.

   if abs_mstr.abs_wt_um = ""
   then
      abs_mstr.abs_wt_um = abs_temp.abs_wt_um.
   else
      if abs_mstr.abs_wt_um <> abs_temp.abs_wt_um
      then do:
         {gprun.i ""gpumcnv.p""
                  "(abs_temp.abs_wt_um,
                    abs_mstr.abs_wt_um,
                    """",
                    output conv_rate)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if conv_rate = ?
         then do:
            /* NO UM CONVERSION EXISTS */
            {pxmsg.i &MSGNUM=33 &ERRORLEVEL=2}
            conv_rate = 1.
         end. /* IF conv_rate = ? */
      end. /* ELSE IF abs_mstr.abs_wt_um <> abs_temp.abs_wt_um */

   abs_mstr.abs_nwt = abs_mstr.abs_nwt + (abs_temp.abs_nwt * conv_rate).
   abs_mstr.abs_gwt = abs_mstr.abs_gwt + (abs_temp.abs_gwt * conv_rate).

   {absupack.i  "abs_temp" 26 22 "l_abs_tare_wt" }

   {absupack.i  "abs_mstr" 26 22 "l_abs_tare_wt1" }

   /* ADD TARE WEIGHT OF MERGED PRE-SHIPPER TO THE CURRENT SHIPPER */
   l_abs_tare_wt1 = l_abs_tare_wt1 + (l_abs_tare_wt * conv_rate).

   {abspack.i   "abs_mstr" 26 22 "l_abs_tare_wt1" }

   /* PICKLIST # ADDED */
   {pxmsg.i &MSGNUM=742 &ERRORLEVEL=1 &MSGARG1=ship_id
            &MSGARG2="""" &MSGARG3=""""}

   /* DELETE CUSTOM SHIPPER DOCUMENT INFORMATION ASSOCIATED WITH THE RECORD. */
   {gprun.i ""sofsde.p"" "(input recid(abs_temp))"}
/*GUI*/ if global-beam-me-up then undo, leave.


   delete abs_temp.

end. /* MAIN */

hide frame a no-pause.

{rcinvcon.i}
{gpnrseq.i}
